<?php 
/**
 * 终端
 * @Author    紫梦
 * @DateTime  2018-02-22
 * @copyright 响响租车
 * @version   1.0
 */
namespace app\index\controller;

use think\Request;
use app\common\request\ClientLai;
use app\common\controller\Bolutu;
use app\common\controller\Execl;
use app\common\model\Terminal as TerminalModel;
use app\common\model\Cars as CarsModel;
use app\common\model\Cars;
use app\common\model\Brand;
use app\common\model\Agency;
use think\Db;
use app\common\model\CarsInfo;
use app\common\model\Version;
use app\common\model\InstallDevices;
use app\common\model\InstallUser;


/**
* 
*/
class Terminal extends Base
{
    
    public function index()
    {
        $request = Request::instance();
        
        if($request->isAjax()){
            $post = $request->post();
            if( !empty($post['datemin']) && !empty($post['datemax']) ){
                $where['create_time'] = ['between time',[$post['datemin'],$post['datemax']]];                       
            }
            if ( empty($post['datemin']) && !empty($post['datemax']) ){             
                $where['create_time'] = ['<= time',$post['datemax']];
                           
            }
            if ( !empty($post['datemin']) && empty($post['datemax']) ) {
                $where['create_time'] = ['> time',$post['datemin']];    
            }
            if ( !empty($post['search_inpute']) ) {
                $where['cardcode|sim_id|equipment_id'] = ['like','%'.$post['search_inpute'].'%'];       
            }
            if ( !empty($post['supplier_no']) ) {
                $where['supplier_no'] = $post['supplier_no'];      
            }
            if ( !empty($post['status']) ) {
                if($post['status']==1){
                    $where['status'] = $post['status'];      
                }else{
                    $where['status'] = null;
                }
            }
            if( empty($where) ){
                $where = '1=1';
            }  
            
            $model= TerminalModel::where($where)
                        ->paginate($post['length'],false,[
                                'type'     => 'bootstrap',
                                'var_page' => 'page',
                                'page'=>$post['start']/$post['length'] +1 ,
                            ]);
            if(count($model)>=1){
                foreach ($model as $key => $value) {
                    $res['data'][] = $value;
                }
                $res['recordsTotal'] = $res['recordsFiltered'] = $model->total();
            }else{
                    $res['data']= '';
                    $res['recordsTotal'] = $res['recordsFiltered'] =0;
            }
            return $res;
        }else{
           return $this->fetch(); 
        }
    }

    /**
     * 基础数据导入
     */
    public function excel_data()
    {
        $request = Request::instance();
        if($request->isPost()){
            $file = $request->file('excel');
            if($file){
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if($info){
                    // 成功上传后 获取上传信息
                    $url = $info->getSaveName();
                    $ext = $info->getExtension();
                    $path = ROOT_PATH . 'public' . DS . 'uploads/'.$url;
                    $excel = new Execl;
                    $excelData = $excel->readerBase($path);
                    
                    if(empty($excelData)){
                        return [
                            'code'=>2,
                            'msg'=>'数据格式不合法'
                            ]; 
                    }
                    $insertNum = 0;
                    // 遍历数据加入到数据库中去 
                    foreach ($excelData as $key => $value) {
                        if($key==0){
                            continue;
                        }else{
                            $data['equipment_id'] = trim($value[0]??'');                            
                            $data['cardcode'] = trim($value[1]??'');                            
                            $data['sim_id'] = trim($value[2]??'');                            
                            
                            switch (trim($value[3]??'')) {
                                case '博鹭途':
                                
                                $data['supplier_no'] = 1376920002;
                                break;
                            case '唯特迅':
                                
                                $data['supplier_no'] = 1376920001;
                                break;      
                            default:
                                $data['supplier_no'] = null;
                                break;
                            }

                            switch (trim($value[4]??'')) {
                                case '已注册':
                                
                                $data['status'] = 1;
                                break;
                            case '未注册':
                                
                                $data['status'] = 0;
                                break;      
                            default:
                                $data['status'] = null;
                                break;
                            }
                            
                            $terminal = TerminalModel::where('equipment_id',$data['equipment_id'])->value('id');
                            if(!$terminal){

                                $res = self::data_add($data);
                                if($res['code']==1){
                                    $insertNum++;
                                }
                            }
                           

                        }

                    }
                }else{
                   return [
                    'code'=>2,
                    'msg'=>$file->getError()
                    ]; 
                }
            }else{
               return [
                    'code'=>2,
                    'msg'=>'没有文件被上传'
                    ]; 
            }

            return [
                'code'=>1,
                'msg'=>'添加了'.$insertNum.'条数据'
                ]; 
        }else{
            return $this->fetch();
        }
    }  

    /**
     * 空表
     */
    public function excel_null()
    {
        Execl::terminal_excel();       
    }

    /**
     * 数据添加
     */
    private static function data_add($data){
        $model = new TerminalModel();
        try {
            $model->allowField(true)->save($data);
        } catch (\Exception $e) {
            return [
                'code'=>2,
                'msg'=>$e->getMessage(),
                ];
        } 
        return [
            'code'=>1,
            'msg'=>'添加成功'
            ];
    }

    /**
     * @return array 版本数组
     */
    public function version()
    {
        $request = Request::instance();
        if($request->isAjax()){
            $post = $request->post();
            $cars = explode('@', $post['cars']);
            $data = Cars::where('car_unique_id','in',$cars)->field('supplier_no,car_unique_id')->select();
            $num = 0;
            if($data){
                foreach ($data as $key => $value) {
                    if($value['supplier_no'] == 1376920002){
                        $config = config('gz_blt');
                        $obj = new ClientLai($config['user_name'],$config['password']);
                        $data['car_unique_id'] = $value['car_unique_id'];                 
                        $data['version'] = $post['version'];
                        $res = $obj->request_post(rtrim($config['url'],'/').'/api/vehicle/updateTerminal',http_build_query($data));
                        $arr = json_decode($res,true);
                        if($arr['result'] == 0){
                            $num ++;

                        }

                    }elseif($value['supplier_no'] == 1376920003){
                        $config = config('local_iot');
                        $obj = new ClientLai($config['user_name'],$config['password']);
                        $res = $obj->request_post(rtrim($config['url'],'/').'/machineSoftware/upgradeDevice',['car_unique_id'=>$value['car_unique_id'],'version'=>$post['version']]);
                        $arr = json_decode($res,true);
                        if($arr['result'] == 0){
                            $num ++;

                        }
                    }
                    
                }

                return [
                    'result'=>0,
                    'msg'=>'已发送('.$num.')辆车'
                ];
                
            }else{
                return [
                    'result'=>1,
                    'msg'=>'车架号不合法'
                ];
            }

        }else{
            // dump(Bolutu::version());
            $cars = $request->get('cars');
            $model = new Version();
            $version = $model->column('version');
            $this->assign('cars',trim($cars,'@'));
            $this->assign('version',$version);
            // $this->assign('version',['1.1.1','1.1.2','1.1.3','1.1.5']);
            return $this->fetch();
        }
    }

    public function cars()
    {
        $request = Request::instance();
        
        if($request->isAjax()){
            $post = $request->post();

            if( !empty($post['datemin']) && !empty($post['datemax']) ){
                $where['create_time'] = ['between time',[$post['datemin'],$post['datemax']]];                  
                    
            }
            if ( empty($post['datemin']) && !empty($post['datemax']) ){             
                $where['create_time'] = ['<= time',$post['datemax']];
                    
                    
            }
            if ( !empty($post['datemin']) && empty($post['datemax']) ) {
                $where['create_time'] = ['> time',$post['datemin']];
                 
            }
            if ( !empty($post['search_inpute']) ) {
                $where['car_sn|car_unique_id|cardcode|equipment_id'] = ['like','%'.$post['search_inpute'].'%'];
                    
            }
            if ( !empty($post['car_usage']) ) {
                $where['car_usage'] = $post['car_usage'];     
                    
            }
            if ( !empty($post['relevance']) ) {
                $where['relevance'] = $post['relevance'];     
                    
            }
            if ( !empty($post['agency_id']) ) {
                $where['agency_id'] = $post['agency_id'];
                    
            }
            if ( !empty($post['brand_id']) ) {
                $where['brand_id'] = $post['brand_id'];
                    
            }
            if ( !empty($post['version']) ) {
                $where['car_unique_id'] = ['in',CarsInfo::where('machine_version',$post['version'])->column('car_unique_id')];
                    
            }
            if(!empty($post['supplier_no'])){
                $where['supplier_no'] = $post['supplier_no'];
            }else{
                $where['supplier_no'] = ['in',[1376920002,1376920003]];
            }

            if(!empty($post['sim_card'])){
                $where['sim_card'] = $post['sim_card'];
            }

            if( empty($where) ){
                $where = '1=1';
            }        
            $model= Cars::with('brand,agency,carsInfo')
                        ->where($where)
                        ->cache(60)
                        ->order('car_id', 'desc')
                        ->field('car_id,car_sn, agency_id,car_unique_id,car_usage,relevance,brand_id,holder_type,holder_id,create_time,band_status,supplier_no,sim_card')
                        ->paginate($post['length'],false,[
                                'type'     => 'bootstrap',
                                'var_page' => 'page',
                                'page'=>$post['start']/$post['length'] +1 ,
                            ]);
            if(count($model)>=1){

                foreach ($model as $key => $value) {
                    $value['agency_name'] = $value->agency['name'];
                    $value['brand_name'] = $value->brand['name'];
                    $res['data'][] = $value;
                }
                
                $res['recordsTotal'] = $res['recordsFiltered'] = $model->total();
            }else{
                    $res['data']= '';
                    $res['recordsTotal'] = $res['recordsFiltered'] =0;
            }
            
            return $res;

        }else{
            
            $model = Cars::count();
            $brand = Brand::all();
            $agency = Agency::where('id','>',1)->field('id,name')->select();
            $this->assign('version',Bolutu::version());
            $this->assign('number',$model);
            $this->assign('brand',$brand);
            $this->assign('agency',$agency);
    
            return $this->fetch();
        }
    }

    /**
     * ajax read terminal data
     * @ return    json
     */
    public function terminal($query = 0)
    {
        $request = Request::instance();
        
        if($request->isAjax()){
            $where = '%'.$query.'%';

            $data = TerminalModel::where('equipment_id','like',$where)->limit(10)->select();

            return $data;
        }
    }

    public function register(){
        $request = Request::instance();
        if($request->isAjax()){
            $param = $request->post();
            $param = trim($param['data'],',');
            $data = TerminalModel::where('id','in',$param)->where('status',null)->column('equipment_id');

            $config = config('iot');
            if($data){
               
                try{
                    $iot_service = new \Mrgoon\AliIot\AliIot($config['key'],$config['pass']);
                    $response = $iot_service->applyDevices($config['productKey'],$data);
                }catch(\Exception $e){
                    return [
                        'code'=>3,
                        'msg'=>$e->getMessage()
                    ]; 
                }
                if($response->Success){
                    try{
                        Db::name('terminal')->where('equipment_id','in', $data)->update(['status'=>1]);

                    }catch(\Exception $e){
                        return [
                            'code'=>2,
                            'msg'=>$e->getMessage()
                        ];
                    }
                    return [
                        'code'=>1,
                        'msg'=>'注册成功'
                    ];
                }else{
                    return [
                        'code'=>2,
                        'msg'=>$response->ErrorMessage
                    ];
                }
            }else{
                return [
                    'code'=>4,
                    'msg'=>'设备已注册'
                ];
            }
        }else{
            $param = $request->param();
            $this->assign('data',trim($param['id'],','));
            return $this->fetch();    
        }
    }

    public function add(){
        $request = Request::instance();
        if($request->isAjax()){
            $param = $request->post();
            $model = new TerminalModel();
            $result = $this->validate($param,
                    [
                       'sim_id'=>'unique:Terminal',
                       'cardcode'=>'unique:Terminal',
                       'equipment_id'=>'unique:Terminal'
                    ]);       
            if(true !== $result){
                return [
                    'code'=>3,
                    'msg'=>'设备已存在'
                ];
            }
            try{
                $model->allowField(true)->save($param);
            }catch(\Exception $e){
                return [
                    'code'=>2,
                    'msg'=>$e->getMessage()
                ];
            }
            return [
                'code'=>1,
                'msg'=>'添加成功'
            ];   
        }else{
            return $this->fetch();
        }
    }

    public function edit($id){
        $request = Request::instance();
        if($request->isAjax()){
            $param = $request->post();
            $model = new TerminalModel();
            $result = $this->validate($param,
                    [
                       'sim_id'=>'unique:Terminal',
                       'cardcode'=>'unique:Terminal',
                       'equipment_id'=>'unique:Terminal'
                    ]);       
            if(true !== $result){
                return [
                    'code'=>3,
                    'msg'=>'设备已存在'
                ];
            }else{
                try{
                    $model->allowField(true)->isUpdate(true)->save($param,['id' => $param['id']]);

                }catch(\Exception $e){
                    return [
                        'code'=>2,
                        'msg'=>$e->getMessage()
                    ];
                }
                return [ 
                    'code'=>1,
                    'msg'=>'更新成功'
                ]; 
            }    
        }else{
            $data = TerminalModel::get($id);
            $this->assign('data',$data);
            return $this->fetch();
            
        }
    }

    public function delete($id){
        try{
            $res = TerminalModel::destroy($id);

        }catch(\Exception $e){
            return [
                'code'=>2,
                'msg'=>$e->getMessage()
            ];
        }
        return [
            'code'=>1,
            'msg'=>'删除成功'
        ];
    }

    public function details($id){
        try{
            $data = Db::name('terminal')->where(['id'=>$id])->find();

        }catch(\Exception $e){
            return [
                'code'=>2,
                'msg'=>$e->getMessage()
            ];
        }
        $config = config('iot');
        try{
            $iot_service = new \Mrgoon\AliIot\AliIot($config['key'],$config['pass']);
            $response = $iot_service->queryDeviceByName($config['productKey'],$data['equipment_id']);
        }catch(\Exception $e){
            return [
                'code'=>3,
                'msg'=>$e->getMessage()
            ]; 
        }
        if($response->Success){
            $data['status'] = $response->DeviceInfo->DeviceStatus;
            $data['time'] = $response->DeviceInfo->GmtCreate;
        }else{
            return [ 
                'code'=>2,
                'msg'=>$response->ErrorMessage
            ];
        }
        $this->assign('data',$data);
        return $this->fetch();
    }

    public function registDevice()
    {
        
        $config = config('local_iot');
        $obj = new ClientLai($config['user_name'],$config['password']);
        $request = Request::instance();
        if($request->isAjax()){
            $param = $request->param();

            try{
                $result = Db::name('terminal')->field('equipment_id,status')->where(['id'=>$param['id']])->find();
            }catch(\Exception $e){
                return [
                    'result'=>2,
                    'msg'=>$e->getMessage()
                ]; 
            }

            if($result){
                if($result['status']==1){
                    return [
                        'result'=>3,
                        'msg'=>'设备已注册'
                    ]; 
                }else{
                    try{
                        $data['device_name'] = $result["equipment_id"];
                        $res = $obj->request_post(rtrim($config['url'],'/').'/device/registDevice',$data);
                        $arr = json_decode($res,true);

                        if($arr['result'] == 0){
                            foreach ($arr['data'] as $value){
                                if($value['success'] == true){
                                    try{
                                        $val = Db::name('terminal')->where(['id'=>$param['id']])->update(['status'=>1]);
                                        if($val){
                                            return [
                                                'result'=>1,
                                                'msg'=>'注册成功'
                                            ];
                                        }
                                    }catch(\Exception $e){
                                        return [
                                            'result'=>4,
                                            'msg'=>$e->getMessage()
                                        ]; 
                                    }
                                    
                                }elseif($value['success'] == false){
                                    $val = Db::name('terminal')->where(['id'=>$param['id']])->update(['status'=>1]);
                                    return [
                                        'result'=>2,
                                        'msg'=>'设备已注册'
                                    ];
                                }
                            }
                        }else{
                            return [
                                'result'=>3,
                                'msg'=>$arr['msg']
                            ];

                        }

                    }catch(\Exception $e){
                        return [
                            'result'=>4,
                            'msg'=>$e->getMessage()
                        ]; 
                    }
                }
            }else{
                return [
                    'result'=>4,
                    'msg'=>'设备号不存在'
                ];
            } 
        } 
    }

    public function batchRegistDevice($id){
        $config = config('local_iot');
        $obj = new ClientLai($config['user_name'],$config['password']);
        $result = Db::name('terminal')->field('id,equipment_id,status')->where('id','in',$id)->select();
        $succ = 0;
        $err = 0;
        foreach ($result as $key => $value) {
            if(!$value['status']==1){

                try{
                    $data['device_name'] = $value["equipment_id"];
                    $res = $obj->request_post(rtrim($config['url'],'/').'/device/registDevice',$data);
                    $arr = json_decode($res,true);

                    if($arr['result'] == 0){
                        foreach ($arr['data'] as $val){
                            if($val['success'] == true){
                                try{
                                    Db::name('terminal')->where(['id'=>$value['id']])->update(['status'=>1]);
                                    $succ++;
                                }catch(\Exception $e){
                                    return [
                                        'result'=>4,
                                        'msg'=>$e->getMessage()
                                    ]; 
                                }
                                
                            }elseif($val['success'] == false){
                                // Db::name('terminal')->where(['id'=>$value['id']])->update(['status'=>1]);
                                $err ++;
                            }
                        }
                    }else{
                        return [
                            'result'=>3,
                            'msg'=>$arr['msg']
                        ];

                    }

                }catch(\Exception $e){
                    return [
                        'result'=>4,
                        'msg'=>$e->getMessage()
                    ]; 
                }
            }
        }
        return [
            'result'=>1,
            'msg'=>'成功('.$succ.')'.'失败('.$err.')'
        ];

    }


    public function queryDeviceDetail($id){

        
        $request = Request::instance();
        $param = $request->param();
        $result = Db::name('terminal')->where(['id'=>$param['id']])->find();
        if($result){
            $arr = $this->queryDetail($result['equipment_id']);
            $cars=null;
            if($arr['result']==0){
                $cars = Cars::where(['equipment_id'=>$result['equipment_id']])->field('band_status,car_unique_id,car_sn,supplier_no')->find();
            }else{
                $arr=null;
            }
        }

        $this->assign('device',$arr['data'][0]['data']);
        $this->assign('cars',$cars);
        return $this->fetch('query_device_detail');
    }
   
    public function current(){
        $CarsInfo = new CarsInfo;
        $model = new Cars;

        $request = Request::instance();
        if($request->isAjax()){
            $param = $request->get();
            $cars = explode('@', $param['cars']);
            $result = Cars::where('car_unique_id','in',$cars)->field('equipment_id,car_unique_id,band_status')->select();
            $num = 0;
            if($result){
                $config = config('gz_blt'); 
                $obj = new ClientLai($config['user_name'],$config['password']);
                foreach ($result as $key => $value) {
                    if($value['band_status'] == 1){
                        return [
                            'result'=>4,
                            'msg'=>'设备未解绑'
                        ];
                    }else{
                        $arr = $this->queryDetail($value['equipment_id']);
                        if($arr['data'][0]['success'] == true){
                            $data['productKey'] = $arr['data'][0]['data']['productKey'];
                            $data['deviceSecret'] = $arr['data'][0]['data']['deviceSecret'];
                            $data['car_unique_id'] = $value['car_unique_id'];
                            $res = $obj->request_post(rtrim($config['url'],'/').'/api/vehicle/updateIot',http_build_query($data));
                            $arr = json_decode($res,true);
                            if($arr['result'] == 0){
                                try{

                                    $CarsInfo->where(['car_unique_id' => $value['car_unique_id']])->update(['supplier_no' => 1376920003]);

                                }catch(\Exception $e){
                                    return [
                                        'result'=>4,
                                        'msg'=>$e->getMessage()
                                    ];
                                }

                                try{

                                    $model->where(['car_unique_id' => $value['car_unique_id']])->update(['supplier_no' => 1376920003]);

                                }catch(\Exception $e){
                                    return [
                                        'result'=>4,
                                        'msg'=>$e->getMessage()
                                    ];
                                }

                                $num++;

                            }else{
                                return [
                                    'result'=>3,
                                    'msg'=>$arr['msg']
                                ];
                            }

                        }else{
                            return [
                                'result'=>3,
                                'msg'=>'设备未注册'
                            ];
                        }
                    }
                      
                }

                return [
                    'result'=>0,
                    'msg'=>'已切换('.$num.')辆车'
                ];

            }else{

                return [
                   'result'=>2,
                    'msg'=>'设备不存在' 
                ];
            }
            
        }

    }

    public function development(){
        $request = Request::instance();
        $config = config('local_iot');
        $obj = new ClientLai($config['user_name'],$config['password']);
        if($request->isAjax()){
            $param = $request->get();
            $cars = explode('@', $param['cars']);
            $result = Cars::where('car_unique_id','in',$cars)->field('equipment_id,car_unique_id')->select();
            $num = 0;
            if($result){
                foreach ($result as $key => $value) {
                    $arr = $this->queryDetail($value['equipment_id']);
                    if($arr['data'][0]['success'] == true){
                        $data['device_name'] = $value['equipment_id'];
                        $data['car_env'] = $param['car_env'];
                        $data['car_unique_id'] = $value['car_unique_id'];
                        $res = $obj->request_post(rtrim($config['url'],'/').'/command/doSetCarEnv',http_build_query($data));
                 
                        $arr = json_decode($res,true);
                        // dump($arr);
                        if($arr['result'] == 0){
                            $num++;
                        }else{
                            return [
                                'result'=>3,
                                'msg'=>$arr['msg']
                            ];
                        }
                        
                    }
                }

                return [
                    'result'=>0,
                    'msg'=>'已切换('.$num.')辆车'
                ];

            }else{
                return [
                   'result'=>2,
                    'msg'=>'设备不存在' 
                ];
            }
        }     
               
    }

    public function queryDetail($param){
        $config = config('local_iot');
        $obj = new ClientLai($config['user_name'],$config['password']);

        $data['device_name'] = $param;
        $res = $obj->request_post(rtrim($config['url'],'/').'/device/queryDeviceDetail',$data);
        if($res){
            $arr = json_decode($res,true);
            
        }else{
            $arr = null;
        }
        
        return $arr; 
        
    }
    
    public function versionAdd(){
        $request = Request::instance();
        $model = new Version();
        if($request->isAjax()){
            $param = $request->post();
            if($param){
                try{

                    $model->allowField(true)->save($param);

                }catch(\Exception $e){
                    return [
                        'result'=>2,
                        'msg'=>$e->getMessage()
                    ];
                }
            }else{
                return [
                    'result'=>2,
                    'msg'=>'版本号不能为空'
                ];
            }

            return [
                'result'=>0,
                'msg'=>'添加成功'
            ];
        }else{
            $version = Version::all();
        
        }

        $this->assign('version',$version);
        return $this->fetch();
    } 

    public function versionDel($id){
        $res = Version::destroy($id);
        if($res) {
            return [
                'result'=>0,
                'msg'=>'删除成功'
            ];

        }else{
            return [
                'result'=>1,
                'msg'=>'删除失败'
            ];
        }
       
    }


    public function ajaxGetVersion($query = 0)
    {
        $request = Request::instance();
        
        if($request->isAjax()){
            $where = '%'.$query.'%';

            $data = CarsInfo::where('machine_version','like',$where)->limit(10)->select();

            return $data;
        }
    }

    public function installUser(){
        $request = Request::instance();
        
        if($request->isAjax()){
            $post = $request->post();
            if( !empty($post['datemin']) && !empty($post['datemax']) ){
                $where['create_time'] = ['between time',[$post['datemin'],$post['datemax']]];                  
                    
            }
            if ( empty($post['datemin']) && !empty($post['datemax']) ){             
                $where['create_time'] = ['<= time',$post['datemax']];
                    
                    
            }
            if ( !empty($post['datemin']) && empty($post['datemax']) ) {
                $where['create_time'] = ['> time',$post['datemin']];
                 
            }

            if ( !empty($post['type']) ) {
                $where['type'] = $post['type'];
                    
            }
            if ( !empty($post['user_id']) ) {
                $where['user_id'] = $post['user_id'];
                    
            }

            if ( !empty($post['search_inpute']) ) {

                $where['car_id'] = ['in',CarsModel::where([
                    'car_sn|car_unique_id|cardcode|equipment_id|sim_id' => ['like','%'.$post['search_inpute'].'%']
                    ])->column('car_id')];
                    
            }

            if( empty($where) ){
                $where = '1=1';
            }   

            $model= InstallDevices::with('cars,terminal,installUser')
                            ->where($where)
                            ->paginate($post['length'],false,[
                                    'type'     => 'bootstrap',
                                    'var_page' => 'page',
                                    'page'=>$post['start']/$post['length'] +1 ,
                                ]);
                    
            if(count($model)>=1){
                foreach ($model as $key => $value) {
                    $res['data'][] = $value;
                }
                
                $res['recordsTotal'] = $res['recordsFiltered'] = $model->total();
            }else{
                $res['data'] = '';
                $res['recordsTotal'] = $res['recordsFiltered'] =0;
            }

           
            return $res;

        }else{
            $model = InstallUser::all();
            $this->assign('data',$model);
            return $this->fetch('installUser');
        }
        
    }

}

// 'http://119.23.61.49:8080'